package com.behome.netty.netty_B.handler;

import com.behome.netty.netty_B.bean.*;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

/**
 * @author bh_ljy
 * @description: 终端心跳回传处理类
 * @date: 2024/5/27 10:36
 */
@Slf4j
public class TerminalBHeartBeatHandler extends BusinessBHandlerBase {

    public TerminalBHeartBeatHandler(ChannelHandlerContext ctx, ReBackPacket msg, int type) {
        super(ctx, msg, type);
    }

    @Override
    public void resolve() {
        try {
            TerminalBHeartBeat heartBeat = (TerminalBHeartBeat) this.msg.getBody().getBusinessReBack();
            log.info(this.ip + ":" + heartBeat.toCHJsonString());
            if (ctx.channel().isActive()) {
                int sessionId = this.msg.getHead().getSessionId();
                ReBackPacket packet = new ReBackPacket();
                ReBackPacketBody body = new ReBackPacketBody(msg.getBody().getSourceCode(), new String[]{msg.getBody().getSourceCode()}, heartBeat);
                packet.setBody(body);
                ReBackPacketHead head = new ReBackPacketHead(sessionId, ReBackPacketHead.PacketType.BEREBACKED, (new Integer(11 + body.serialize().length + 4)).shortValue());
                packet.setHead(head);
                ReBackPacketTail tail = new ReBackPacketTail(head, body);
                packet.setTail(tail);
                this.ctx.writeAndFlush(packet);
            }
        } catch (Exception e) {
            log.error("发生异常", e);
        }
    }
}